package com.iretailer.report.web.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.ReHeatChatRule;
import com.iretailer.report.model.HeatChatRule;
import com.iretailer.report.model.ImageData;
import com.iretailer.report.model.enums.ImageTag;
import com.iretailer.report.service.HeatChatRuleService;
import com.iretailer.report.service.ImageDataService;
import com.iretailer.report.service.UserMallService;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @program: report
 * @description:
 * @author: Bennie
 * @create: 2020-07-01 16:30
 **/


@RestController
public class HeatChatRuleController {

    @Autowired
    private UserMallService userMallService;

    @Autowired
    private HeatChatRuleService heatChatRuleService;

    @Autowired
    private ImageDataService imageDataService;

    @RequestMapping("/system/pageHeatChatRule.action")
    public Map<String, Object> pageHeatChatRule(@RequestBody ReHeatChatRule reHeatChatRule,HttpServletRequest request) {
        PageResult<HeatChatRule> result = heatChatRuleService.pageHeatChatRule(reHeatChatRule,SecurityUserUtil.currentUser(request).getId());
        return RMap.ok(result);
    }

    @RequestMapping("/system/listMyMalls.action")
    public Map<String, Object> listMyMalls(HttpServletRequest request) {
        String userId = SecurityUserUtil.currentUser(request).getId();
        if("Root".equals(userId)){
            return RMap.ok(userMallService.selectAllMalls());
        }
        List<Map<String, Object>> result =  userMallService.selectMallsByUserIds(Collections.singletonList(userId));
        return RMap.ok(result);
    }

    @RequestMapping("/system/listOptionalImages.action")
    public Map<String, Object> listOptionalImages() {
        List<ImageData> result = imageDataService.list(new QueryWrapper<ImageData>().lambda()
                .eq(ImageData::getImageTag, ImageTag.HEAT_CHAT_RULE.getName()));
        return RMap.ok(result);
    }


    @RequestMapping("/system/saveOrUpdateRule.action")
    public Map<String, Object> saveOrUpdateRule(@RequestBody ReHeatChatRule reHeatChatRule,HttpServletRequest request) {
        boolean result = heatChatRuleService.saveOrUpdateRule(reHeatChatRule,SecurityUserUtil.currentUserName(request));
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/deleteRule.action")
    public Map<String, Object> deleteRule(@RequestBody List<String> ids) {
        boolean result = heatChatRuleService.removeByIds(ids);
        return RMap.resultBoolean(result);
    }

}
